package com.sibu.yx.promotion.web.support.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.doubo.security.servlet.AbstractSecurityFilter;
import com.doubo.security.subject.Subject;
import com.doubo.security.util.ThreadContext;

/**
 * Created with IntelliJ IDEA.
 * User: qiujingwang
 * Date: 2017/4/6
 * Description:
 */
public class SecurityTokenUidFilter extends AbstractSecurityFilter {

    protected Subject getSubject(HttpServletRequest request, HttpServletResponse response) {
        /*String token = request.getHeader(AppApiConstants.HEAD_TOKEN);
        String uid = request.getHeader(AppApiConstants.HEAD_UID);
        if(StringUtil.isNumeric(uid)){
            return new TokenUidSubject(Long.parseLong(uid), token);
        }
        return new TokenUidSubject(null, token);*/

        return new TokenUidSubject(request, response);
    }

    @Override
    protected void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {
        ThreadContext.bind(getSubject(getHttpServletRequest(servletRequest), getHttpServletResponse(servletResponse)));
        try {
            super.doFilterInternal(servletRequest, servletResponse, chain);
        } finally {
            ThreadContext.remove();
        }
    }
}
